# -*- coding: utf-8 -*-
import reprlib

from common import trace


class MyList:
    def __init__(self, data=[]):
        self._data = data

    def __len__(self):
        return len(self._data)

    def __getitem__(self, index):
        if isinstance(index, int):
            return self._data[index]
        raise KeyError('index must be a int')

    def __setitem__(self, key, value):
        try:
            self._data[key] = value
        except IndexError as e:
            print(e)
            pass

    def __delitem__(self, key):
        self._data = self._data[0:key] + self._data[key + 1:]

    def __reversed__(self):
        # return reversed(self._data)
        return MyList(self._data[::-1])

    def __repr__(self):
        return 'MyList(%s)' % reprlib.repr(self._data)

    @trace
    def __contains__(self, item):
        return item in self._data

    def append(self, value):
        self._data.append(value)

    def insert(self, index, value):
        self._data.insert(index, value)


my_lst = MyList([1, 2, 3])
my_lst[3] = 3
print(reversed(my_lst))
# print(my_lst['0'])
print(2 in my_lst)


class MyDict(dict):

    @trace
    def __missing__(self, key):
        """只有当Ｃlass继承 dict时, 并且找不到时才运行"""
        return None


dic = MyDict()
print(dic['a'])
dic.update(name='vincent', age="20")
print(dic['name'])
